#include "stdafx.h"
#include "Printer.h"
#include "ComDefinition.h"

using namespace Utility::LoggerLib;

CPOSPrinter::CPOSPrinter(int nType, int nPort)
: m_nType(nType),
  m_nPort(nPort),
  m_nError(0),
  m_nParity(2),
  m_hPort(NULL)
{

}

BOOL CPOSPrinter::Initialize()
{
    if(m_nPort >= COM1 && m_nPort <= COM6)
    {
        Log(DEBUG_LEVEL, EPOSChecker, _T("Initialize Printer, using COM%d, and the parity is %d."), m_nPort, m_nParity);
        m_hPort = PR_OpenCom(m_nPort, m_oWrite, m_oRead, m_nParity, m_nError);
    }
    else if (m_nPort >= LPT1 && m_nPort <= LPT3)
    {
        // Open LPT
        int nLPT = m_nPort - COM6;
        Log(DEBUG_LEVEL, EPOSChecker, _T("Initialize Printer, using LPT%d."), nLPT);
        m_hPort = PR_OpenLPT(nLPT, m_oWrite, m_oRead, m_nError);
    }
    return NULL == m_hPort ? FALSE: TRUE;
}

BOOL CPOSPrinter::Print(LPCTSTR pStr, int nLength, int nColor /* = 1 */)
{
    if(m_nPort > EUnkownPort && m_nPort < EPortCount)
    {
        if (1 == nColor)
        {
            SendString(_T("\x1br\x00"),3*sizeof(TCHAR));
        }
        else if (2 == nColor)
        {
            SendString(_T("\x1br\x01"),3*sizeof(TCHAR));
        }
        
        SendString(pStr, nLength);

        return SendString(_T("\x0a"), 1*sizeof(TCHAR));
    }

    else if (m_nPort >= LPT1 && m_nPort <= LPT3)
    {
        Log(ERROR_LEVEL, EPOSChecker, _T("LPT%d was not supported. "), m_nPort);
        return FALSE;
    }
    
    return TRUE;
}

BOOL CPOSPrinter::Close()
{
    if (NULL == m_hPort)
        return TRUE;

    if (m_nPort >= COM1 && m_nPort <= COM6)
    {
        return PR_CloseCom(m_hPort, m_oWrite, m_oRead);
    }
    else if (m_nPort >= LPT1 && m_nPort <= LPT3)
    {
        return PR_CloseLPT(m_hPort, m_oWrite, m_oRead);
    }

    return FALSE;
}

BOOL CPOSPrinter::SendString(LPCTSTR lpStr, int nLen)
{
    if (NULL == m_hPort) return FALSE;

    if(m_nPort > EUnkownPort && m_nPort < EPortCount)
    {
        // Send to COM
        return PR_SendStr2Com(m_hPort, lpStr, nLen, m_oWrite);
    }

    return FALSE;
}

//////////////////////////////////////////////////////////////////////////
// CPT900
CPT900::CPT900(int nPort)
: CPOSPrinter(T90, nPort)
{

}

BOOL CPT900::Initialize()
{
    CPOSPrinter::Initialize();
    return SendString(_T("\x1b\x40"),2*sizeof(TCHAR));
}

//////////////////////////////////////////////////////////////////////////////
// CPOS88VI
CPOS88VI::CPOS88VI(int nPort)
    : CPOSPrinter(POS88VI, nPort)
{
}

BOOL CPOS88VI::Initialize()
{
    CPOSPrinter::Initialize();

    return TRUE;
}

void CPOS88VI::SetPrintMode(int nMode)
{
    
}

void CPOS88VI::SetUnderline(int nUL)
{
    char szCmd[32];
    memset(szCmd, '\0', 32);
    sprintf(szCmd, "\x1b\x2d%x", nUL);

    PR_SendStr2Com(m_hPort, szCmd, 32, m_oWrite);
}

void CPOS88VI::EnablePrinter(BOOL bEnable)
{
    SendString(_T("x\1b\x3d"), 2*sizeof(TCHAR));
    if(TRUE == bEnable)
    {
        SendString(_T("x\00"), sizeof(TCHAR));
    }
    else
    {
        SendString(_T("x\01"), sizeof(TCHAR));
    }
}

void CPOS88VI::CutPaper(int nMode)
{
    char szCmd[32];
    memset(szCmd, '\0', 32);

    if(nMode == 0 || nMode == 1 || nMode == 48 || nMode == 49)
    {
        sprintf(szCmd, ("\x1d\x56\x01"));
    }
    else if(nMode == 66)
    {
        sprintf(szCmd, ("\x1d\x56\x66"));
    }

    PR_SendStr2Com(m_hPort, szCmd, 32, m_oWrite);
    
}

void CPOS88VI::EnableBold(BOOL bEnable)
{
    char szCmd[32];
    memset(szCmd, '\0', 32);

    if(TRUE == bEnable)
    {
        sprintf(szCmd, ("\x1b\x45\x01"));
    }
    else
        sprintf(szCmd, ("\x1b\x45\x00"));

    PR_SendStr2Com(m_hPort, szCmd, 32, m_oWrite);
}


//////////////////////////////////////////////////////////////////////////////
// CPR58
CPR58::CPR58(int nPort)
    : CPOSPrinter(PR58, nPort)
{
}

BOOL CPR58::Initialize()
{
    CPOSPrinter::Initialize();
    return SendString(_T("\x1b\x40"),2*sizeof(TCHAR));
}

void CPR58::PrintandFeed(LPCTSTR pStr, int nLength, int nFeed)
{

    if(m_nPort > EUnkownPort && m_nPort < EPortCount)
    {

        SendString(pStr, nLength);

        char szCmd[32];
        memset(szCmd, '\0', 32);
        sprintf(szCmd, "\x1b\x4a%x", nFeed);
        PR_SendStr2Com(m_hPort, szCmd, 32, m_oWrite);
    }

    else if (m_nPort >= LPT1 && m_nPort <= LPT3)
    {
        Log(ERROR_LEVEL, EPOSChecker, _T("LPT%d was not supported. "), m_nPort);
    }
}

void CPR58::SetDefualLineHight()
{
    SendString(_T("\x1b\x32"),2*sizeof(TCHAR));
}

void CPR58::SetLineHeight(int nHeight)
{
    char szCmd[32];
    memset(szCmd, '\0', 32);
    sprintf(szCmd, "\x1b\x33%x", nHeight);
    PR_SendStr2Com(m_hPort, szCmd, 32, m_oWrite);
}

void CPR58::SetPrintMode(int nMode)
{
    char szCmd[32];
    memset(szCmd, '\0', 32);
    sprintf(szCmd, "\x1b\x21%x", nMode);
    PR_SendStr2Com(m_hPort, szCmd, 32, m_oWrite);
}

void CPR58::EnableDoubleWidth()
{
    SendString(_T("\x1b\x0e"),2*sizeof(TCHAR));
}

void CPR58::DisableDoubleWidth()
{
    SendString(_T("\x1b\x14"),2*sizeof(TCHAR));
}

void CPR58::EnableUserFontSet(BOOL bEnable /* = TRUE */)
{

}

void CPR58::EnableButton(BOOL bEnable /* = TRUE */)
{
    char szCmd[32];
    memset(szCmd, '\0', 32);
    sprintf(szCmd, "\x1b\x63\x35%x", bEnable);
    PR_SendStr2Com(m_hPort, szCmd, 32, m_oWrite);
}

void CPR58::OpenCashdrawer(int nOpenInter /* = 0 */, int nCloseInter /* = 0 */)
{
    char szCmd[32];
    memset(szCmd, '\0', 32);
    sprintf(szCmd, "\x1b\x70\x00%x%x", nOpenInter, nCloseInter);
    PR_SendStr2Com(m_hPort, szCmd, 32, m_oWrite);
}

int CPR58::QureyCashdrawerStatus()
{
    // 1B 75
    return 0;
}

BOOL CPR58::QueryStatus(int nUnit, int &nError)
{
    return TRUE;
}
